package org.tity.commons.concurrent.fork;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;

public class Fork {
	
	public static void main(String[] args) {
		
		//生成1-1000的数组
	    long[] longs = LongStream.rangeClosed(1, 999999L).toArray();

	    long t1=System.currentTimeMillis();
	    //计算1+2+...+1000的值
	    RecursiveTask<Long> recursiveTask = new ForkJoinSumCounter(longs);
	    Long invoke = new ForkJoinPool().invoke(recursiveTask);

	    long t2=System.currentTimeMillis();
	    //计算1+2+...+1000的值
	    long forSum = 0;
	    for (long aLong : longs) {
	        forSum += aLong;
	    }
	    long t3=System.currentTimeMillis();

	    //对比看计算出来的值是否相同
	    System.out.println("ForkJoinSum("+(t2-t1)+"):" + invoke + ", for("+(t3-t2)+"):" + forSum + ", check:" + (invoke == forSum));
		
	}
	
}
 